package com.jshcbd.modules.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jshcbd.modules.asset.dto.InventoryItemDto;
import com.jshcbd.modules.asset.entity.AssetEntity;
import com.jshcbd.modules.asset.entity.InventoryEntity;
import com.jshcbd.modules.asset.entity.InventoryItemEntity;
import com.jshcbd.modules.asset.mapper.AssetMapper;
import com.jshcbd.modules.asset.mapper.InventoryItemMapper;
import com.jshcbd.modules.asset.mapper.InventoryMapper;
import com.jshcbd.modules.asset.service.IInventoryService;
import com.jshcbd.modules.asset.vo.InventoryItemVo;
import com.jshcbd.modules.asset.vo.InventoryProgressVo;
import com.jshcbd.modules.asset.vo.InventoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Service
@Transactional
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, InventoryEntity> implements IInventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private AssetMapper assetMapper;

    @Autowired
    private InventoryItemMapper inventoryItemMapper;


    @Override
    public IPage<InventoryVo> pageView(Page<InventoryVo> page, QueryWrapper<InventoryVo> queryWrapper) {
        return inventoryMapper.queryPageView(page,queryWrapper);
    }

    @Override
    public void doSave(InventoryEntity entity) {
        this.save(entity);
        //String mainId,String contractNamber,String companyId,Integer building,Integer floor
        inventoryItemMapper.generateItemByScope(entity.getId(),
                entity.getScopeContractNamber(),
                entity.getScopeCompanyId(),
                entity.getScopeLocationBuilding(),
                entity.getScopeLocationFloor());

    }

    @Override
    public InventoryItemEntity getItemById(String id) {
        return inventoryItemMapper.selectById(id);
    }

    @Override
    public IPage<InventoryItemVo> itemPageView(Page<InventoryItemVo> page, QueryWrapper<InventoryItemVo> queryWrapper) {
        return inventoryItemMapper.queryPageView(page,queryWrapper);
    }

    @Override
    public List<InventoryItemVo> itemView(QueryWrapper<InventoryItemVo> queryWrapper) {
        return inventoryItemMapper.queryView(queryWrapper);
    }

    @Override
    public void updateAssetItemState(InventoryItemDto dto) {
        inventoryItemMapper.updateState(dto.getId(),dto.getItemState(),dto.getUseState());

    }

    @Override
    public InventoryProgressVo getProgress(String mainId) {
        Integer total = inventoryItemMapper.selectCount(new QueryWrapper<InventoryItemEntity>().eq("main_id",mainId));
        Integer completed = inventoryItemMapper.selectCount(new QueryWrapper<InventoryItemEntity>().eq("main_id",mainId).eq("item_state",2));
        return new InventoryProgressVo(total,completed);
    }

    @Override
    public void doInventoryEnd(String id) {


        inventoryMapper.updateInventoryEnd(id);
        //将损坏状态更新到资产信息中
        inventoryMapper.updateAssetByInventory(id);
    }

    @Override
    public InventoryItemVo doInventory(String mainId, String addressId,String rfid) {
        //更具rfid获取资产信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rfid",rfid);
        AssetEntity entity = assetMapper.selectOne(queryWrapper);
        if(entity ==  null){
            return null;
        }
        queryWrapper = new QueryWrapper();
        queryWrapper.eq("main_id",mainId);
        queryWrapper.eq("asset_id",entity.getId());
        //获取当前盘点资产信息
        InventoryItemEntity itemEntity   = inventoryItemMapper.selectOne(queryWrapper);
        if(itemEntity == null){//如果当前盘点范围没有不存资产，是盘盈
            itemEntity = new InventoryItemEntity();
            itemEntity.setMainId(mainId);
            itemEntity.setAddressId(addressId);
            itemEntity.setAssetId(entity.getId());
            itemEntity.setUseState(entity.getUseState());
            itemEntity.setItemState(3);

        }else{
            itemEntity.setItemState(2);
            itemEntity.setAddressId(addressId);
        }
        inventoryItemMapper.updateById(itemEntity);
        return inventoryItemMapper.queryViewById(itemEntity.getId());
    }

    @Override
    public void updateItem(InventoryItemEntity entity) {
        inventoryItemMapper.updateById(entity);
    }
}
